package com.itedge.solutionmanager.web.taskhandler.impl.customer;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

import com.itedge.solutionmanager.domain.impl.ContactPerson;
import com.itedge.solutionmanager.domain.impl.Customer;
import com.itedge.solutionmanager.service.domain.IContactPersonService;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.infrastructure.service.task.ITaskService;
import com.itedge.infrastructure.service.taskdata.ITaskDataEntityService;
import com.itedge.infrastructure.stereotype.WebHandler;
import com.itedge.solutionmanager.taskdata.impl.customer.RemoveContactsTaskData;
import com.itedge.solutionmanager.web.convert.ISOFormattingConversionServiceBean;
import com.itedge.infrastructure.web.taskhandler.TaskHandlerException;
import com.itedge.infrastructure.web.taskhandler.impl.AbstractTaskDataHandler;

@WebHandler
public class RemoveContactsTaskDataHandler extends AbstractTaskDataHandler<RemoveContactsTaskData, Customer> {

    private IContactPersonService contactPersonService;
    private ITaskDataEntityService<RemoveContactsTaskData> removeContactsTaskDataService;
    
    @Autowired
    protected RemoveContactsTaskDataHandler(ISOFormattingConversionServiceBean conversionService,
    		IProcessService<Customer> customerProcessService,
    		ITaskDataEntityService<RemoveContactsTaskData> removeContactsTaskDataService,
    		ITaskService<RemoveContactsTaskData, Customer> removeContactsTaskService,
			MessageSource messageSource, IContactPersonService contactPersonService) {
		super(conversionService, customerProcessService, removeContactsTaskDataService, 
				removeContactsTaskService, messageSource);
		this.contactPersonService = contactPersonService;
		this.removeContactsTaskDataService = removeContactsTaskDataService;
	}
	
	@Override
	protected Class<RemoveContactsTaskData> getTaskDataClass() {
		return RemoveContactsTaskData.class;
	}
    
	@Override
	public Map<String,Object> loadTaskData(String taskId, Customer linkedCustomer, Boolean forHistory) {
    	RemoveContactsTaskData taskData = removeContactsTaskDataService.findEntityByProcessTaskId(taskId);
    	if (taskData == null) {
    	    taskData = new RemoveContactsTaskData();
    	} 
    	//if not for history, set actual contacts
    	if (Boolean.FALSE.equals(forHistory)) {
    		taskData.setRelatedContacts(linkedCustomer.getContactPersons());	
    	}
    	taskData.setTaskId(taskId);
    	Map<String,Object> data = new HashMap<String,Object>();
    	data.put("taskData", taskData);
    	return data;	
	}
	
	@Override
	public RemoveContactsTaskData bindTaskData(HttpServletRequest request, Customer linkedCustomer) 
			throws TaskHandlerException {
		RemoveContactsTaskData taskData = super.bindTaskData(request, linkedCustomer);
		bindRelatedContactsFromCustomer(taskData,linkedCustomer);
		return taskData;
	}
	
	private void bindRelatedContactsFromCustomer(RemoveContactsTaskData taskData, Customer linkedCustomer) {
        Set<ContactPerson> copiedContacts = new HashSet<ContactPerson>();
        for (ContactPerson p: linkedCustomer.getContactPersons()) {
        	ContactPerson newContact = contactPersonService.getContactPersonReference(p.getId());
        	copiedContacts.add(newContact);
        }
        taskData.setRelatedContacts(copiedContacts);		
	}
	
}
